/**
 * 发送短信工具类
 */
package com.naiterui.ehp.bp.utils.notice;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;

/**
 * 发送短信工具类
 *
 * @since 2.9.2
 */
public class SendMessageUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(SendMessageUtil.class);

    private String url;

    public SendMessageUtil(String url) {
        super();
        if (StringUtils.isAnyBlank(url)) {
            LOGGER.warn("[短信]--初始化失败url:{}", url);
            throw new IllegalArgumentException("[短信]--初始化失败url is not any blank");
        }
        this.url = url;
    }

    /**
     * 单条短信发送
     */
    public void sendSms(String phone, String templateId, Object[] datas) {
        if (!StringUtil.isMobileNum(phone)) {
            LOGGER.warn("[发送短信]--非法的手机号电话号码:{} 模板ID:{} 参数：{}", phone, templateId, datas);
            return;
        }
        try {
            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("phone", phone);
            paramsMap.put("templateId", templateId);
            if (datas != null) {
                paramsMap.put("datas", datas);
            }
            LOGGER.info("调用发送短信接口：url: {}, params: {}", this.url, JsonMapper.toJson(paramsMap));
            RestHttpUtil.post().url(this.url).paramMap(paramsMap).executeData();
        } catch (Exception e) {
            LOGGER.error("[发送短信]--发送短信失败,电话号码:{} 模板ID:{} 参数：{}", phone, templateId, datas, e);
        }

    }

}
